//
//  XCYAudioTool.m
//  01-播放音效
//
//  Created by 冯华强 on 15/10/8.
//  Copyright (c) 2015年 HQ. All rights reserved.
//

#import "XCYAudioTool.h"
#import <AVFoundation/AVFoundation.h>

static NSMutableDictionary *_soundIDs;

static NSMutableDictionary *_players;

static AVAudioPlayer *playering;

@interface XCYAudioTool ()

@end

@implementation XCYAudioTool

+ (NSMutableDictionary *)soundIDs
{
    if (_soundIDs == nil) {
        _soundIDs = [NSMutableDictionary dictionary];
    }
    return _soundIDs;
}

+ (NSMutableDictionary *)players
{
    if (_players == nil) {
        _players = [NSMutableDictionary dictionary];
    }
    return _players;
}

+ (void)playAudioWithFileName:(NSString *)fileName
{
    if (fileName == nil) return;
    
    //    从字典中获取
    SystemSoundID soundID = [[[self soundIDs] objectForKey:fileName] unsignedIntValue];
    if (!soundID) {
        
        NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
        
        if (url == nil) return;
        
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
        
        [[self soundIDs] setObject:@(soundID) forKey:fileName];
    }
    
    //仅播放音效
    AudioServicesPlaySystemSound(soundID);
}

/**
 * 销毁音效
 */
+ (void)disposeSystemSoundIDWithFileName:(NSString *)fileName
{
    if (fileName == nil) return;
    
    SystemSoundID soundID = [[[self soundIDs] objectForKey:fileName] unsignedIntValue];
    if (soundID) {
        AudioServicesDisposeSystemSoundID(soundID);
        [[self soundIDs] removeObjectForKey:fileName];
    }
}

+ (AVAudioPlayer *)playingAndPasueMusicWithFileName:(NSString *)fileName isBundle:(BOOL)isBundle numberOfLoops:(NSInteger)numberOfLoops
{
    if (fileName == nil ) return nil;
    
    //从字典中获取
    AVAudioPlayer *player = [[self players] objectForKey:fileName];
    
    if (player && player.playing){  //暂停
        player = [self pasueMusicWithFileName:fileName];
    }else{  //播放
        player = [self playingMusicWithFileName:fileName isBundle:isBundle];
    }
    player.numberOfLoops = numberOfLoops;
    return player;
}

+ (AVAudioPlayer *)playingAndPasueMusicWithFileName:(NSString *)fileName isBundle:(BOOL)isBundle
{
    return [self playingAndPasueMusicWithFileName:fileName isBundle:isBundle numberOfLoops:0];
}

+ (AVAudioPlayer *)playingMusicWithFileName:(NSString *)fileName isBundle:(BOOL)isBundle
{
    if (fileName == nil) return nil;
    
    //从字典中获取
    AVAudioPlayer *player = [[self players] objectForKey:fileName];
    
    if (!player) {
        NSURL *url = nil;
        if (isBundle) {
            url = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
        }else{
            url = [NSURL fileURLWithPath:fileName];
        }
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        playering = player;
        
        //缓冲音乐
        if (![playering prepareToPlay]) return nil;
        
        [self players][fileName] = player;
    }
    
    if (!playering.playing) {
        //播放音乐
        [playering play];
    }
    return player;
}

+ (AVAudioPlayer *)pasueMusicWithFileName:(NSString *)fileName
{
    if (fileName == nil ) return nil;
    
    //从字典中获取
    AVAudioPlayer *player = [[self players] objectForKey:fileName];
    
    if (player == nil || !player.playing) return nil;
    
    //暂停
    [player pause];
    return player;
}

+ (void)stopMusicWithFileName:(NSString *)fileName
{
    if (fileName == nil) return;
    
    //从字典中获取
    AVAudioPlayer *player = [[self players] objectForKey:fileName];
    
    if (player == nil) return;
    
    [player stop];
    [[self players] removeObjectForKey:fileName];
    player = nil;
}

+ (void)stopAllMusic
{
    for (NSString *key in [self.players allKeys]) {
        [self stopMusicWithFileName:key];
    }
}

@end
